//
//  YYNavigationBar.m
//  YYNavigationBar
//
//  Created by yangyang on 16/7/29.
//  Copyright © 2016年 机智的静默( http://www.cnblogs.com/jingmo/ ). All rights reserved.
//

#import "YYNavigationBar.h"
#import "YYNavigationItem.h"
#import "YYNavigationMacro.h"

#import "NSObject+YYHelper.h"

@interface YYNavigationBar ()

@property (nonatomic) CGFloat stackCount;
@property (nonatomic, weak) UIViewController *lastVC;
@property (nonatomic) UIImageView *bgImageView;
@property (nonatomic, readwrite) YYNavigationItem *yy_navigationItem;

@end

@implementation YYNavigationBar

#pragma mark - InitMethod

+ (instancetype)naviBarWithStackCount:(CGFloat)stackCount lastViewController:(UIViewController *)lastViewController {
    YYNavigationBar *navigationBar = [[YYNavigationBar alloc] initWithStackCount:stackCount lastViewController:lastViewController];
    return navigationBar;
}

- (instancetype)initWithStackCount:(CGFloat)stackCount lastViewController:(UIViewController *)lastViewController {
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, [NSObject yy_naviBarWidth], [NSObject yy_naviBarHeight]);
        
        self.bgImageView = [[UIImageView alloc] initWithFrame:self.bounds];
        self.bgImageView.backgroundColor = [UIColor darkGrayColor];
        [self addSubview:self.bgImageView];
        
        self.stackCount = stackCount;
        self.lastVC = lastViewController;
        
        self.naviAlpha = 1.0;
        self.naviSuperAlpha = 1.0;
        self.naviBgColor = [UIColor clearColor];
        
        [self addSubview:self.yy_navigationItem];
    }
    
    return self;
}

#pragma mark - SetterMethod

- (void)setIsHiddenNaviBar:(BOOL)isHiddenNaviBar {
    _isHiddenNaviBar = isHiddenNaviBar;
    self.hidden = isHiddenNaviBar;
}

- (void)setNaviBgColor:(UIColor *)naviBgColor {
    if (!naviBgColor || [naviBgColor isEqual:[UIColor clearColor]]) {
        _naviBgColor = [UIColor clearColor];
        self.backgroundColor = [UIColor clearColor];
    } else {
        _naviBgColor = naviBgColor;
        self.bgImageView.image = nil;
    }
    self.bgImageView.backgroundColor = _naviBgColor;
}

- (void)setNaviBgImage:(UIImage *)naviBgImage {
    _naviBgImage = naviBgImage;
    self.naviBgColor = [UIColor clearColor];
    self.bgImageView.image = self.naviBgImage;
}

- (void)setNaviAlpha:(CGFloat)naviAlpha {
    _naviAlpha = naviAlpha;
    self.alpha = naviAlpha;
}

- (void)setNaviSuperAlpha:(CGFloat)naviSuperAlpha {
    _naviSuperAlpha = naviSuperAlpha;
    self.backgroundColor = [self.naviBgColor colorWithAlphaComponent:naviSuperAlpha];
    self.bgImageView.alpha = naviSuperAlpha;
}

#pragma mark - GetterMethod

- (YYNavigationItem *)yy_navigationItem {
    if (!_yy_navigationItem) {
        _yy_navigationItem = [YYNavigationItem naviItemWithStackCount:self.stackCount lastViewController:self.lastVC];
    }
    return _yy_navigationItem;
}

@end
